import type { ToolInstallVersionItem } from "@/modules/dev-env/common/types.ts";
import path from "@/framework/util/path.ts";

export function compareJdkVersion(version1: string, version2: string): -1 | 0 | 1 {
  // 使用正则表达式将版本号分割成数组，并转换为整数
  const v1Parts = version1.split(".").map(Number);
  const v2Parts = version2.split(".").map(Number);

  // 获取两个版本号数组中最长的部分长度
  const maxLength = Math.max(v1Parts.length, v2Parts.length);

  // 对比每个部分
  for (let i = 0; i < maxLength; i++) {
    // 如果当前索引超过了某个版本号的长度，则认为该部分为0
    const v1Part = i < v1Parts.length ? v1Parts[i] : 0;
    const v2Part = i < v2Parts.length ? v2Parts[i] : 0;

    // 比较当前部分
    if (v1Part > v2Part) {
      return 1; // version1 大于 version2
    } else if (v1Part < v2Part) {
      return -1; // version1 小于 version2
    }
  }

  // 如果所有部分都相等，则认为两个版本号相等
  return 0;
}

/**
 * 解析DragonwellJDK版本信息
 *
 * @code
 *  ``` json5
 *  // 数据格式
 *  {
 *    version8: "8.20.21";
 *    xurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_x64_linux.tar.gz";
 *    aurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_aarch64_linux.tar.gz";
 *    wurl8: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/8.20.21-test-dragonwell_standard/Alibaba_Dragonwell_Standard_8.20.21_x64_windows.zip";
 *    type8: "oss";
 *    version11: "11.0.24.20.8";
 *    xurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_linux.tar.gz";
 *    aurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_aarch64_linux.tar.gz";
 *    apurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_alpine-linux.tar.gz";
 *    wurl11: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.24.20.8-test-dragonwell_standard/Alibaba_Dragonwell_Standard_11.0.24.20.8_x64_windows.zip";
 *    rurl11: "";
 *    type11: "oss";
 *    version17: "17.0.12.0.13.7";
 *    xurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_linux.tar.gz";
 *    aurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_aarch64_linux.tar.gz";
 *    apurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_alpine-linux.tar.gz";
 *    wurl17: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/17.0.12.0.13%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_17.0.12.0.13.7_x64_windows.zip";
 *    type17: "oss";
 *    version21: "21.0.4.0.4.7";
 *    xurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_linux.tar.gz";
 *    aurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_aarch64_linux.tar.gz";
 *    apurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_alpine-linux.tar.gz";
 *    wurl21: "https://dragonwell.oss-cn-shanghai.aliyuncs.com/21.0.4.0.4%2B7-test-dragonwell_standard/Alibaba_Dragonwell_Standard_21.0.4.0.4.7_x64_windows.zip";
 *    type21: "oss";
 *  }
 * ```
 * @param versionInfo
 * @param edition
 */
export function parseDragonwellVersions(versionInfo: any, edition: string): ToolInstallVersionItem[] {
  let keys = Object.keys(versionInfo);
  switch (native.process.platform) {
    case "win32":
      keys = keys.filter(it => it.startsWith("wurl"));
      break;
    case "linux":
      keys = keys.filter(it => it.startsWith("xurl") || it.startsWith("apurl"));
      break;
  }
  return keys
    .filter(it => versionInfo[it])
    .map(it => {
      const url = versionInfo[it];
      const versionCode = it.replace(/^[a-zA-Z]+/, "");
      const version = versionInfo[`version${versionCode}`];
      return {
        id: `dragonwell-${edition}-${version}`,
        name: path.basename(url),
        version,
        edition,
        url,
      };
    });
}
